---
title: "conjoint MM"
author: "Dai Yamao"
date: "`r format(Sys.time(), '%Y-%m-%d')`"
output:
  html_document: default
  pdf_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.width = 10, fig.height = 5)
rm(list=ls())

require(tidyverse)
require(cjoint)
library(cregg)
library(scales)
library(patchwork)
library(extrafont)
require(memisc) 

dat_cjoint <- read_csv("data/conjoint_data_tr.csv")

dat_cjoint <- dat_cjoint %>% 
  mutate(Sect = dplyr::recode(Sect,
                      "1" = "sunni",
                      "2" = "shia",
                      "3" = "christian",
                      "4" = "kurd",
                      "5" = "others"))

dat_cjoint <- dat_cjoint %>% 
  mutate(sunni = ifelse(Sect == 1, 1, 0),
         shia = ifelse(Sect == 2, 1, 0),
         kurd = ifelse(Sect == 4, 1, 0),
         christian = ifelse(Sect == 3, 1, 0),
         minority = ifelse(Sect == 5, 1, 0))

dat_cjoint$Q3_1[dat_cjoint$Q3_1 == 6] <- NA
dat_cjoint$Q3_2[dat_cjoint$Q3_2 == 6] <- NA
dat_cjoint$Q3_3[dat_cjoint$Q3_3 == 6] <- NA
dat_cjoint$Q3_7[dat_cjoint$Q3_7 == 6] <- NA

dat_cjoint <- dat_cjoint %>% 
  mutate(trust_president = 6 - Q3_1,
         trust_pm = 6 - Q3_2,
         trust_parliament = 6 - Q3_3,
         trust_party = 6 - Q3_7)
dat_cjoint <- dat_cjoint %>% 
  mutate(trust_gov = (trust_president + trust_pm + trust_parliament + trust_party) /4)

dat_cjoint <- dat_cjoint %>% 
  mutate(Trust_gov = case_when(trust_gov < 2 ~ "low",
                               trust_gov >= 2 & trust_gov < 5 ~ "middle",
                               trust_gov >= 5 ~ "high"))

dat_cjoint <- dat_cjoint %>% 
  mutate(gotovote = dplyr::recode(Q2,
                      "1" = "will vote",
                      "2" = "will not vote"))

dat_cjoint[,c(3:6, 18:22, 29:34)] <- data.frame(lapply(dat_cjoint[,c(3:6, 18:22, 29:34)],as.factor))
str(dat_cjoint)
```

# conjoint setting and whole model
```{r}
# setting
f1 <- choice ~ place + sect + reason + timing + organizer
f2 <- choice ~ sect + reason + organizer
f3 <- choice ~ sect
f4 <- choice ~ sect + organizer
f5 <- choice ~ reason

#dat_cjoint$place <- factor(dat_cjoint,
#                           levels = c("Capital", "Other", "Provincial capital"))
dat_cjoint$sect <- factor(dat_cjoint$sect, 
                          levels = c("Sunni", "Shia", "Kurd", "Minority", "Mixture"))
dat_cjoint$reason <- factor(dat_cjoint$reason, 
                          levels = c("Unemployment", "Wealfare", "Public goods", "Inflation", 
                           "Environment", "Corruption", "Political system", 
                           "Election fraus", "Intervention"))
dat_cjoint$timing <- factor(dat_cjoint$timing,
                            levels = c("Non-election", "During-campaign", "Election day", "Post-election") )
dat_cjoint$organizer <- factor(dat_cjoint$organizer, 
                          levels = c("Sadr", "Imtidad", "Non-party", "Tribal chief", "Non-organizer"))


whole_cj <- cj(dat_cjoint, f1, 
               id = id,
               estimate = "mm", 
               h0 = 0.5)
plot(whole_cj, vline = 0.5)
whole_cj

# amce
whole_amce <- cj(data = dat_cjoint, f1,
                 id = ~ id,
                 estimate = "amce")
whole_amce
plot(whole_amce, vline = 0,
     size = 2)

# plot estimation difference
whole_cj$Estimate <- "MM"
whole_amce$Estimate <- "AMCE"
plot(rbind(whole_cj, whole_amce), feature_headers = FALSE, size = 2)+
  ggplot2::facet_wrap(~ Estimate, ncol = 2L)
```

# by sect
```{r, fig.height = 10, dpi = 300, warning = FALSE}
reg01 <- cj(data = dat_cjoint,
            choice ~ place + sect + reason + timing + organizer,
            id = id,
            estimate = "mm",
            by = ~ Sect)
reg01
plot(reg01, group = "Sect", vline = 0.5)

# anova
cj_anova(data = dat_cjoint, 
         choice ~ place + sect + reason + timing + organizer + Sect, by = ~ Sect)

# plot
sect_amce <- cj(data = dat_cjoint,
                choice ~ place + sect + reason + timing + organizer,
                id = ~ id,
                estimate = "amce",
                by = ~ Sect)
plot(sect_amce, size = 2) +
  ggplot2::facet_wrap(~ BY, ncol = 3L)

# OLS result
write_html(sect_amce, file = "result/sect_amce.html")

plot(cj_freqs(dat_cjoint,
              f1,
              id = ~ id))

# other models
cj_sect <- cj(dat_cjoint, f3, 
               id = id,
               estimate = "mm", 
               h0 = 0.5, 
               by = ~ Sect)
plot(cj_sect, vline = 0.5, group = "Sect")

cj_sect_org <- cj(dat_cjoint, f4, 
               id = id,
               estimate = "mm", 
               h0 = 0.5, 
               by = ~ Sect)
plot(cj_sect_org, vline = 0.5, group = "Sect")

cj_reason <- cj(dat_cjoint, f5, 
               id = id,
               estimate = "mm", 
               h0 = 0.5, 
               by = ~ Sect)
plot(cj_reason, vline = 0.5, group = "Sect")
```

# subset by sect
```{r}
# sunni
fig1_sunni <- dat_cjoint %>%
  filter(Sect == "sunni")  %>%
  cj(f1, id = id, estimate = "mm", h0 = 0.5)
plot(fig1_sunni, vline = 0.5)

# shia
fig1_shia <- dat_cjoint %>%
  filter(Sect == "shia")  %>%
  cj(f1, id = id, estimate = "mm", h0 = 0.5)
plot(fig1_shia, vline = 0.5)

# kurd
fig1_kurd <- dat_cjoint %>%
  filter(Sect == "kurd")  %>%
  cj(f1, id = id, estimate = "mm", h0 = 0.5)
plot(fig1_kurd, vline = 0.5)

# minoritis
fig1_mino <- dat_cjoint %>%
  filter(Sect == "christian" | Sect == "other")  %>%
  cj(f1, id = id, estimate = "mm", h0 = 0.5)
plot(fig1_mino, vline = 0.5)
```

# sect by gender (sect, reason, organizer)
```{r, fig.height = 10}
whole_cj_organizer <- cj(dat_cjoint, f2, 
               id = id,
               estimate = "mm", 
               h0 = 0.5, 
               by = ~ Sect)
plot(whole_cj_organizer, vline = 0.5, group = "Sect")

# sunni
fig2_sunni <- dat_cjoint %>%
  filter(Sect == "sunni")  %>%
  cj(f2, id = id, estimate = "mm", h0 = 0.5,
     by = ~ Gender)
plot(fig2_sunni, vline = 0.5, group = "Gender")

# shia
fig2_shia <- dat_cjoint %>%
  filter(Sect == "shia")  %>%
  cj(f2, id = id, estimate = "mm", h0 = 0.5, 
     by = ~ Gender)
plot(fig2_shia, vline = 0.5, group = "Gender")

# kurd
fig2_kurd <- dat_cjoint %>%
  filter(Sect == "kurd")  %>%
  cj(f2, id = id, estimate = "mm", h0 = 0.5,
     by = ~ Gender)
plot(fig2_kurd, vline = 0.5, group = "Gender")

# minoritis
fig2_mino <- dat_cjoint %>%
  filter(Sect == "christian" | Sect == "other")  %>%
  cj(f2, id = id, estimate = "mm", h0 = 0.5,
     by = ~ Gender)
plot(fig2_mino, vline = 0.5, group = "Gender")
```

# education
```{r}
reg02 <- cj(data = dat_cjoint,
            choice ~ place + sect + reason + timing + organizer,
            id = id,
            estimate = "mm",
            by = ~ Education)
reg02
plot(reg02, group = "Education", vline = 0.5)

cj_anova(data = dat_cjoint, 
         choice ~ place + sect + reason + timing + organizer + Education, by = ~ Education)

# subset
lowedu <- dat_cjoint %>% 
  filter(Education == "4" | Education == "3" | Education == "2" | Education == "1") %>% 
  cj(f1, id = id, estimate = "mm", h0 = 0.5)
plot(lowedu, vline = 0.5)

highedu <- dat_cjoint %>% 
  filter(Education == "5" | Education == "6" | Education == "7") %>% 
  cj(f1, id = id, estimate = "mm", h0 = 0.5)
plot(highedu, vline = 0.5)


highedu_sect <- dat_cjoint %>% 
  filter(Education == "5" | Education == "6" | Education == "7") %>% 
  cj(f2, id = id, estimate = "mm", h0 = 0.5,
     by = ~ Sect)
plot(highedu_sect, vline = 0.5, grouo = "Sect")
```
# sect: education
```{r}
lowedu_shia <- dat_cjoint %>% 
  filter(Education == "4" | Education == "3" | Education == "2" | Education == "1") %>% 
  filter(Sect == "shia") %>% 
  cj(f1, id = id, estimate = "mm", h0 = 0.5)
plot(lowedu_shia, vline = 0.5)

highedu_shia <- dat_cjoint %>% 
  filter(Education == "5" | Education == "6" | Education == "7") %>% 
  filter(Sect == "shia") %>% 
  cj(f1, id = id, estimate = "mm", h0 = 0.5)
plot(highedu_shia, vline = 0.5)

```

# income
```{r}
reg03 <- cj(data = dat_cjoint,
            choice ~ place + sect + reason + timing + organizer,
            id = id,
            estimate = "mm",
            by = ~ Income)
reg03
plot(reg03, group = "Income", vline = 0.5)

cj_anova(data = dat_cjoint, 
         choice ~ place + sect + reason + timing + organizer + Income, by = ~ Income)
```

# voting
```{r}
dat_cjoint$gotovote <- factor(dat_cjoint$gotovote)

reg04 <- cj(data = dat_cjoint,
            choice ~ place + sect + reason + timing + organizer,
            id = id,
            estimate = "mm",
            by = ~ gotovote)
reg04
plot(reg04, group = "gotovote", vline = 0.5)

cj_anova(data = dat_cjoint, 
         choice ~ place + sect + reason + timing + organizer + gotovote, by = ~ gotovote)
```

# trust
```{r}
reg05 <- cj(data = dat_cjoint,
            choice ~ place + sect + reason + timing + organizer,
            id = id,
            estimate = "mm",
            by = ~ trust_gov)
reg05
plot(reg05, group = "trust_gov", vline = 0.5)

cj_anova(data = dat_cjoint, 
         choice ~ place + sect + reason + timing + organizer + trust_gov, by = ~ trust_gov)

fig1_low <- dat_cjoint %>%
  filter(trust_gov == 1.25 | trust_gov == 1)  %>%
  cj(f1, id = id, estimate = "mm", h0 = 0.5)
plot(fig1_low, vline = 0.5)


reg06 <- dat_cjoint %>%
  filter(Trust_gov == "low")  %>%
  cj(f1, id = id, estimate = "mm", h0 = 0.5)
plot(reg06, vline = 0.5)
```



